//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "LineSearch.h"
using namespace Cephei::QL::Math::Optimization;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Math/Optimization/Problem.h>
#include <gen/QL/Math/Optimization/EndCriteria.h>
#include <gen/QL/Math/Optimization/Constraint.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Math::Optimization::CLineSearch::CLineSearch (boost::shared_ptr<QuantLib::LineSearch>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLineSearch = NULL;
#endif
	_ppLineSearch = &childNative;
    
}
Cephei::QL::Math::Optimization::CLineSearch::CLineSearch (QuantLib::LineSearch& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phLineSearch = NULL;
#endif
	_ppLineSearch = new boost::shared_ptr<QuantLib::LineSearch> (&childNative);
    
    _LineSearchOwner = owner;
    
}

Cephei::QL::Math::Optimization::CLineSearch::CLineSearch (CLineSearch^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLineSearch = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppLineSearch = new boost::shared_ptr<QuantLib::LineSearch> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Optimization::CLineSearch::CLineSearch (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLineSearch = NULL;
#endif
	if (!t->IsSubclassOf(CLineSearch::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Optimization::CLineSearch::CLineSearch (QuantLib::Handle<QuantLib::LineSearch>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phLineSearch = &childNative;
	_ppLineSearch = &static_cast<boost::shared_ptr<QuantLib::LineSearch>>(childNative.currentLink());
    
    _LineSearchOwner = owner;
}
Cephei::QL::Math::Optimization::CLineSearch::CLineSearch (QuantLib::Handle<QuantLib::LineSearch> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phLineSearch = &childNative;
	_ppLineSearch = &static_cast<boost::shared_ptr<QuantLib::LineSearch>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Optimization::CLineSearch::CLineSearch (QuantLib::LineSearch childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLineSearch = NULL;
#endif
	_ppLineSearch = new boost::shared_ptr<QuantLib::LineSearch> (new QuantLib::LineSearch (childNative));
    
}
#endif

Cephei::QL::Math::Optimization::CLineSearch::~CLineSearch ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppLineSearch != NULL)
    {
	    delete _ppLineSearch;
        _ppLineSearch = NULL;
    }
}
Cephei::QL::Math::Optimization::CLineSearch::!CLineSearch ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppLineSearch != NULL)
    {
	    delete _ppLineSearch;
    }
}
QuantLib::LineSearch& Cephei::QL::Math::Optimization::CLineSearch::GetReference ()
{
    if (_ppLineSearch == NULL) throw gcnew NativeNullException ();
	return **_ppLineSearch;
}
boost::shared_ptr<QuantLib::LineSearch>& Cephei::QL::Math::Optimization::CLineSearch::GetShared ()
{
    if (_ppLineSearch == NULL) throw gcnew NativeNullException ();
	return *_ppLineSearch;
}
QuantLib::LineSearch* Cephei::QL::Math::Optimization::CLineSearch::GetPointer ()
{
    if (_ppLineSearch == NULL) throw gcnew NativeNullException ();
	return &**_ppLineSearch;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::LineSearch>& Cephei::QL::Math::Optimization::CLineSearch::GetHandle ()
{
	if (_phLineSearch == NULL)
	{
		_phLineSearch = new Handle<QuantLib::LineSearch> (*_ppLineSearch);
	}
	return *_phLineSearch;
}
#endif
bool Cephei::QL::Math::Optimization::CLineSearch::HasNative () 
{
	return (_ppLineSearch != NULL);
}

Double Cephei::QL::Math::Optimization::CLineSearch::LastFunctionValue::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppLineSearch)->lastFunctionValue ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IArray^ Cephei::QL::Math::Optimization::CLineSearch::LastGradient::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Array& _rv = (QuantLib::Array&)(*_ppLineSearch)->lastGradient ( );   
        Cephei::QL::Math::CArray^ _nrv = gcnew Cephei::QL::Math::CArray (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Optimization::CLineSearch::LastGradientNorm2::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppLineSearch)->lastGradientNorm2 ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IArray^ Cephei::QL::Math::Optimization::CLineSearch::LastX::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Array& _rv = (QuantLib::Array&)(*_ppLineSearch)->lastX ( );   
        Cephei::QL::Math::CArray^ _nrv = gcnew Cephei::QL::Math::CArray (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::IArray^ Cephei::QL::Math::Optimization::CLineSearch::SearchDirection::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Array& _rv = (QuantLib::Array&)(*_ppLineSearch)->searchDirection ( );   
        Cephei::QL::Math::CArray^ _nrv = gcnew Cephei::QL::Math::CArray (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Math::Optimization::CLineSearch::Succeed::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	bool _rv = (bool)(*_ppLineSearch)->succeed ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Optimization::CLineSearch::Update (Cephei::QL::Math::IArray^ p_params, Cephei::QL::Math::IArray^ direction, Double beta, Cephei::QL::Math::Optimization::IConstraint^ constraint)
{
    CArray^ _Cp_params;
    CArray^ _Cdirection;
    CConstraint^ _Cconstraint;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cp_params = safe_cast<CArray^> (p_params);
        _Cp_params->Lock();
        QuantLib::Array& _p_params = static_cast<QuantLib::Array&> (_Cp_params->GetReference ()); 
        _Cdirection = safe_cast<CArray^> (direction);
        _Cdirection->Lock();
        QuantLib::Array& _direction = static_cast<QuantLib::Array&> (_Cdirection->GetReference ()); 
        QuantLib::Real _beta = (QuantLib::Real)ValueHelper::Convert (beta);
        _Cconstraint = safe_cast<CConstraint^> (constraint);
        _Cconstraint->Lock();
        QuantLib::Constraint& _constraint = static_cast<QuantLib::Constraint&> (_Cconstraint->GetReference ()); 
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppLineSearch)->update ( _p_params,  _direction,  _beta,  _constraint );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cp_params != nullptr) _Cp_params->Unlock();
        if (_Cdirection != nullptr) _Cdirection->Unlock();
        if (_Cconstraint != nullptr) _Cconstraint->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

